﻿@page "/roles"
@using Csla.Blazor
@using  Csla.Rules
@using ProjectTracker.Library.Admin
@inject ViewModel<RoleEditList> vm
@attribute [HasPermission(AuthorizationActions.EditObject, typeof(RoleEditList))]

<h1>Roles</h1>

<p class="text-danger">@vm.ViewModelErrorText</p>

@if (vm.Model == null)
{
  <p>Loading...</p>
}
else
{
  <AuthorizeView Policy="@(CslaPolicy.GetPolicy(AuthorizationActions.CreateObject, typeof(ProjectTracker.Library.Admin.RoleEditList)))">
    <p><a href="javascript: void(0);" @onclick="AddRole">Add role</a></p>
  </AuthorizeView>
  <table class="table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      @if (addingRole)
      {
        <tr>
          <td>new</td>
          <td>
            <TextInput Property="@(new PropertyInfo(newRole, nameof(newRole.Name)))" />
          </td>
          <td>
            <a class="alert-warning" href="javascript: void(0);" @onclick="AddAccepted">Confirm</a>
            <span> | </span>
            <a href="javascript: void(0);" @onclick="AddCancelled">Cancel</a>
          </td>
        </tr>
      }
      @foreach (var item in vm.Model)
      {
        <tr>
          @if (item.Id == itemSelectedForDeletion)
          {
            <td>@item.Id</td>
            <td>@item.Name</td>
            <td>
              <a class="alert-danger" href="javascript: void(0);" @onclick="() => Delete(item.Id)">Confirm</a>
              <span> | </span><a href="javascript: void(0);" @onclick="() => itemSelectedForDeletion = -1">Cancel</a>
            </td>
          }
          else if (item.Id == itemSelectedForEdit)
          {
            <td>@item.Id</td>
            <td>
              <TextInput Property="@(new PropertyInfo(item, nameof(item.Name)))" />
            </td>
            <td>
              <a class="alert-warning" href="javascript: void(0);" @onclick="EditAccepted">Confirm</a>
              <span> | </span>
              <a href="javascript: void(0);" @onclick="EditCancelled">Cancel</a>
            </td>
          }
          else
          {
            <td>@item.Id</td>
            <td>@item.Name</td>
            <AuthorizeView Policy="@(CslaPolicy.GetPolicy(AuthorizationActions.EditObject, typeof(ProjectTracker.Library.Admin.RoleEditList)))">
              <Authorized>
                <td>
                  <a href="javascript: void(0);" @onclick="() => SelectForEdit(item.Id)">Edit</a>
                  <span> | </span>
                  <a href="javascript: void(0);" @onclick="() => SelectForDelete(item.Id)">Delete</a>
                </td>
              </Authorized>
              <NotAuthorized>
                <td></td>
              </NotAuthorized>
            </AuthorizeView>
          }
        </tr>
      }
    </tbody>
  </table>
}

@code {
  private bool addingRole = false;
  private RoleEdit newRole;
  private int itemSelectedForDeletion = -1;
  private int itemSelectedForEdit = -1;

  protected override async Task OnParametersSetAsync()
  {
    await vm.RefreshAsync(() => Csla.DataPortal.FetchAsync<RoleEditList>());
  }

  private void AddRole()
  {
    itemSelectedForDeletion = -1;
    EditCancelled();
    newRole = Csla.DataPortal.CreateChild<RoleEdit>();
    addingRole = true;
  }

  private async void AddAccepted()
  {
    addingRole = false;
    vm.Model.Add(newRole);
    await SaveAndRefresh();
  }

  private void AddCancelled()
  {
    addingRole = false;
    newRole = null;
  }

  private void SelectForEdit(int id)
  {
    addingRole = false;
    EditCancelled();
    itemSelectedForDeletion = -1;
    itemSelectedForEdit = id;
    var item = vm.Model.Where(r => r.Id == id).FirstOrDefault();
    if (item != null)
      item.BeginEdit();
  }

  private async void EditAccepted()
  {
    var item = vm.Model.Where(r => r.Id == itemSelectedForEdit).FirstOrDefault();
    if (item != null)
      item.ApplyEdit();
    itemSelectedForEdit = -1;
    await SaveAndRefresh();
  }

  private void EditCancelled()
  {
    if (itemSelectedForEdit > -1)
    {
      var item = vm.Model
        .Where(r => r.Id == itemSelectedForEdit)
        .FirstOrDefault();
      if (item != null)
        item.CancelEdit();
      itemSelectedForEdit = -1;
    }
  }

  private void SelectForDelete(int id)
  {
    addingRole = false;
    EditCancelled();
    itemSelectedForDeletion = id;
  }

  private async void Delete(int id)
  {
    itemSelectedForDeletion = -1;
    var item = vm.Model.Where(r => r.Id == id).FirstOrDefault();
    if (item != null)
      vm.Model.Remove(item);
    await SaveAndRefresh();
  }

  private async Task SaveAndRefresh()
  {
    await vm.SaveAsync();
    await vm.RefreshAsync(() => Csla.DataPortal.FetchAsync<RoleEditList>());
    StateHasChanged();
  }
}
